package com.mls.fivedistrict.controller.portal;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.entity.request.CreateOrderRequest;
import com.mls.fivedistrict.entity.request.PayOrderRequest;
import com.mls.fivedistrict.entity.response.CreateOrderResponse;
import com.mls.fivedistrict.enums.GoodsOrderStatusEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.GoodsOrder;
import com.mls.fivedistrict.server.GoodsOrderService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/5/18 13:49
 */
@RestController
@RequestMapping("/api/app")
public class GoodsOrderController extends BaseController {
    @Autowired
    GoodsOrderService goodsOrderService;

    /**
     * 订单创建
     * @param createOrderRequest
     * @return
     */
    @PostMapping("/v1/goodsOrder")
    public ServiceResponse createGoodsOrder(@RequestBody CreateOrderRequest createOrderRequest) {
        BeanValidator.check(createOrderRequest);
        createOrderRequest.setUserId(getUser().getId());
        CreateOrderResponse order = goodsOrderService.createOrder(createOrderRequest);
        return ServiceResponse.createSuccessResponse(order);
    }

    @PutMapping("/v1/goodsOrder/cancel/{goodsOrderId}")
    public ServiceResponse cancelGoodsOrder(@PathVariable Long goodsOrderId) {
        goodsOrderService.cancelOrder(goodsOrderId, getUser().getId());
        return ServiceResponse.createSuccessResponse();
    }

    /**
     * 订单支付
     * @param payOrderRequest
     * @return
     */
    @PostMapping("/v1/goodsOrder/pay")
    public ServiceResponse payOrder(@RequestBody PayOrderRequest payOrderRequest) {
        BeanValidator.check(payOrderRequest);
        payOrderRequest.setUserId(getUser().getId());
        boolean b = goodsOrderService.payOrder(payOrderRequest);
        return ServiceResponse.createSuccessResponse();
    }


    /**
     * 删除订单
     *
     * @param goodsOrderId
     * @return
     */
    @DeleteMapping("/v1/goodsOrder/{goodsOrderId}")
    public ServiceResponse deleteGoodsOrder(@PathVariable Long goodsOrderId) {
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("删除订单的id不能为空"));
        GoodsOrder goodsOrder = new GoodsOrder();
        goodsOrder.setId(goodsOrderId);
        goodsOrder.setIsDelete(true);
        boolean flag = goodsOrderService.updateById(goodsOrder);
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除订单成功");
        } else {
            return ServiceResponse.createErrorResponse("删除订单失败");
        }
    }

    /**
     * 根据用户id查询订单列表
     *
     * @return
     */
    @GetMapping("/v1/goodsOrders")
    public ServiceResponse getGoodsOrderList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        Wrapper wrapper = pageFilter.getWrapper();
        if (wrapper == null) {
            wrapper = Condition.create();
        }
        wrapper.eq("user_id", getUser().getId());
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = goodsOrderService.selectList(wrapper);
        PageInfo goodsOrderPageInfo = new PageInfo<GoodsOrder>(list, 5);
        return ServiceResponse.createSuccessResponse(goodsOrderPageInfo);
    }

    /**
     * 根据id查询订单
     *
     * @param goodsOrderId
     * @return
     */
    @GetMapping("/v1/goodsOrder/{goodsOrderId}")
    public ServiceResponse getGoodsOrder(@PathVariable Long goodsOrderId) {
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("查询订单id不能为空"));
        Condition condition = Condition.create();
        condition.eq("user_id", getUser().getId());
        condition.eq("id", goodsOrderId);
        GoodsOrder goodsOrder = goodsOrderService.selectOne(condition);
        if (null != goodsOrder) {
            return ServiceResponse.createSuccessResponse(goodsOrder);
        } else {
            return ServiceResponse.createErrorResponse("该订单不存在");
        }
    }
}
